package cn.java75.sp.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author oldliu
 * @since 1.0
 */
//@Component//
//@ServletComponentScan

// @WebFilter(urlPatterns = "/*",filterName = "timeFilter")
public class TimeFilter extends OncePerRequestFilter {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
            String path=httpServletRequest.getServletPath();
            long start=System.nanoTime();
            filterChain.doFilter(httpServletRequest,httpServletResponse);
            long end=System.nanoTime();
            System.out.println(path+"访问，耗时："+(end-start)+"纳秒");
            //
        redisTemplate.opsForList().rightPush(path,new java.util.Date()+">>>"+(end-start));
    }
}
